import {
	OpenIMSDK
} from 'open-im-sdk';
import * as http from './http.js';
import md5 from 'md5';

export const openIM = new OpenIMSDK();


function toast(text) {
	uni.showToast({
		title: text,
		icon: 'none',
	});
}

/// 登录
export const loginIM = (phoneNumber, password) => {
	let loginInfo = {
		phoneNumber: phoneNumber,
		password: md5(password),
		platform: 5
	}
	return http.appServerLogin(loginInfo).then((res) => {
		let config = {
			uid: res.uid,
			token: res.token,
			url: "ws://121.37.25.71:30000",
			platformID: 5,
		};
		return openIM.login(config).then(res => {
			console.log("login suc...");
			return res;
		}).catch(err => {
			console.log("login failed...");
			console.log(err);
			toast(err.errMsg);
		});
	}).catch((e) => {
		toast(e.errMsg);
	});
}

// 获取会话列表
export const getConversationList = () => {
	return openIM.getAllConversationList().then(res => {
		return res;
	}).catch(err => {
		console.log(err);
	})
}

// 获取用户详细信息
export const getUserInfo = (userList) => {
	return openIM.getUsersInfo(userList).then(res => {
		console.log(res);
		return res;
	}).catch(err => {

	})
}


// 退出登录
export const loginOut = () => {
	openIM.logout().then(res => {
		console.log(res);
	}).catch(err => {

	})
}

// 查询登录状态
export const checkLoginStatus = () => {
	return openIM.getLoginStatus().then(res => {
		console.log(res);
		return res == '101' ? true : false;
	}).catch(err => {
		console.log(err);
	});
}

// 添加好友
export const addTheFriend = (uid) => {
	return openIM.addFriend(uid).then(res => {
		console.log(res);
		return res;
	}).catch(err => {
		console.log(err);
	})
}


// 获取历史消息
export const getHistoryMessages = (userID, startM, groupID) => {
	let options = {
		groupID: groupID ?? '',
		startMsg: startM,
		count: 20,
		userID: userID ?? ''
	}
	console.log(options);
	return openIM.getHistoryMessageList(options).then(res => {
		return res;
	}).catch(err => {

	})
}


// 发送文字消息
export const sendTextMessage = (text, targetId) => {
	return openIM.createTextMessage(text).then(res => {
		return res.data;
	}).catch(err => {

	})
}


// 发送消息
export const sendMessage = (message, targetId) => {
	const data = {
		recvID: targetId,
		groupID: "",
		onlineUserOnly: false,
		message: message,
	};
	return openIM.sendMessage(data).then(res => {
		return res;
	}).catch(err => {

	})
}

// 发送消息
export const sendMessageNOSS = (message, targetId) => {
	const data = {
		recvID: targetId,
		groupID: "",
		onlineUserOnly: false,
		message: message,
	};
	return openIM.sendMessageNotOss(data).then(res => {
		return res;
	}).catch(err => {

	})
}


//发送语音消息
export const sendImageMessage = (pic, targetId) => {
	const baseInfo = {
		uuid: "xxx",
		type: "png",
		size: 12465,
		width: 1080,
		height: 720,
		url: "http://www.ptbird.cn/usr/uploads/2019/07/2213642051.png"
	}
	const data = {
		sourcePicture: baseInfo,
		bigPicture: baseInfo,
		snapshotPicture: baseInfo,
	}
	return openIM.createImageMessage(data).then(res => {

	}).catch(err => {

	})
}

/// 标记会话已读
export const markSingleMessageRead = (uid,groupId) => {
	if(uid != null){
		return openIM.markSingleMessageHasRead(uid).then(res => {
			return res;
		}).catch(err => {})
	}else{
		return openIM.markGroupMessageHasRead(groupID).then(res => {
			return res;
		})
	}
}
